Home:ALL Converter>Column order of a datagridview from SQL Server database in WinForms using C#

Column order of a datagridview from SQL Server database in WinForms using C#

Ask Time:2021-03-14T12:56:23         Author:alan smith

Json Formatter

I am trying to make my datagridview show data in descending order (from Z to A) based only on one column (name). When the form is loaded, in order to achieve that, I have created a stored procedure with the clause ORDER BY:

CREATE PROC ORDENAR
    @IDCATEGORIA INT,
    @NOMBRE NVARCHAR(30),
    @DESCRIPCION NVARCHAR(256)
AS
    SELECT IDCATEGORIA, NOMBRE 
    FROM clasificacion
    WHERE NOMBRE = @NOMBRE
    ORDER BY NOMBRE DESC;

Then in my C# code, I call that stored procedure:

public void ordenarcategoria(E_CATEGORIA categoria)
{
    using (var conexion = new SqlConnection(ConfigurationManager.ConnectionStrings["conectar"].ConnectionString))
    {
        conexion.Open();

        SqlCommand CMD = new SqlCommand("ORDENAR", conexion);
        CMD.CommandType = CommandType.StoredProcedure;
        CMD.Parameters.AddWithValue("@NOMBRE","");
        CMD.Parameters.AddWithValue("@IDCATEGORIA", 1);
        CMD.Parameters.AddWithValue("DESCRIPCION", "");

        CMD.ExecuteNonQuery();
        conexion.Close();
    }
}

I want my datagridview to show data in descending order with a stored procedure in SQL Server.

I am using WinForms, and here's how my datagridview is loaded:

public void mostrarbuscar(string buscar)
{
    N_clasificacion objn = new N_clasificacion();
    Tablaclasificacion.DataSource = objn.listandoC(buscar);
     
    Tablaclasificacion.ClearSelection();
}

private void fmrClasificacion_Load(object sender, EventArgs e)
{
    mostrarbuscar("");
    accionestabla();
}

tablaclasificacion is the name of my DGV

Author:alan smith,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/66621243/column-order-of-a-datagridview-from-sql-server-database-in-winforms-using-c-shar
yy